import { createApp } from 'vue';
import { services } from '@front/web-basic';
import App from './App';
import apis from './api';
import router, { setupRouter, setupRouterGuard } from './router';
import { registerGlobComp } from './components/registerGlobComp';
import { setupStore } from './store';

import { setEntryParams, login, checkBed } from './logic';
import { ENTRY_PARAMS } from './constants/common';
import Storage from './utils/storage';
import XltData from './utils/XltData';

import { createSentry } from './utils/sentry';

import { registerDirective } from './utils/directives';

import './index.scss';

// 初始化api
services.registerApi(apis);

(async () => {
  const app = createApp(App);

  if (process.env.NODE_ENV === 'production') {
    createSentry(app, router);
  }

  // 保存入口参数
  setEntryParams();

  // 挂载 store
  setupStore(app);

  // 挂载公共组件
  registerGlobComp(app);

  // 注册指令
  registerDirective(app);

  // 检查床位
  if (!Storage.get(ENTRY_PARAMS)?.isPreview) {
    await checkBed();
  }

  // 挂载路由
  setupRouter(app);

  // 路由守卫监听
  setupRouterGuard();

  XltData.init();

  if (!Storage.get(ENTRY_PARAMS)?.isPreview) {
    await login();
  }

  await router.isReady();

  app.mount('#root');
})();
